import { fetchUserCenter } from '../../services/usercenter/fetchUsercenter';
import Toast from 'tdesign-miniprogram/toast/index';
const app = getApp();
const menuData = [
	[
		{
			title: '收货地址',
			tit: '',
			url: '',
			type: 'address',
		},
		{
			title: '优惠券',
			tit: '',
			url: '',
			type: 'coupon',
		},
		{
			title: '积分',
			tit: '',
			url: '',
			type: 'point',
		},
	],
	[
		{
			title: '帮助中心',
			tit: '',
			url: '',
			type: 'help-center',
		},
		{
			title: '客服热线',
			tit: '',
			url: '',
			type: 'service',
			icon: 'service',
		},
	],
];

const orderTagInfos = [
	{
		title: '待付款',
		iconName: 'wallet',
		orderNum: 0,
		tabType: 5,
		status: 1,
	},
	{
		title: '待发货',
		iconName: 'deliver',
		orderNum: 0,
		tabType: 10,
		status: 1,
	},
	{
		title: '待收货',
		iconName: 'package',
		orderNum: 0,
		tabType: 40,
		status: 1,
	},
	{
		title: '待评价',
		iconName: 'comment',
		orderNum: 0,
		tabType: 50,
		status: 1,
	},
	{
		title: '已完成',
		iconName: 'exchang',
		orderNum: 0,
		tabType: 51,
		status: 1
	},
];

const getDefaultData = () => ({
	showMakePhone: false,
	isLogin: false,
	userInfo: {
		avatarUrl: '',
		nickName: '未登录',
		phoneNumber: '',
	},
	menuData,
	orderTagInfos,
	customerServiceInfo: {},
	currAuthStep: 1,
	showKefu: true,
	versionNo: '',
});

Page({
	data: getDefaultData(),

	onLoad() {
		this.setData({ isLogin: app.globalData.isLogin });
		this.getVersionInfo();
	},

	onShow() {
		const isLogin = app.globalData.isLogin;
		this.setData({ isLogin: isLogin, currAuthStep: isLogin ? 2 : 1 });
		this.getTabBar().init();
		this.init();
	},
	onPullDownRefresh() {
		this.init();
	},

	init() {
		if (app.globalData.isLogin) {
			this.fetUseriInfoHandle();
		} else {
			this.setData({
				userInfo: {},
				currAuthStep: 1,
			});
		}
	},

	fetUseriInfoHandle() {
		fetchUserCenter().then(({ userInfo, countsData, orderTagInfos: orderInfo, customerServiceInfo }) => {
			menuData?.[0].forEach((v) => {
				countsData.forEach((counts) => {
					if (counts.type === v.type) {
						v.tit = counts.num;
					}
				});
			});
			const info = orderTagInfos.map((v, index) => ({
				...v,
				...orderInfo[index],
			}));

			this.setData({
				userInfo,
				menuData,
				orderTagInfos: info,
				customerServiceInfo,
				currAuthStep: this.data.isLogin ? 2 : 1,
			});
			wx.stopPullDownRefresh();
		});
	},

	onClickCell({ currentTarget }) {
		const { type } = currentTarget.dataset;

		switch (type) {
			case 'address': {
				wx.navigateTo({ url: '/pages/usercenter/address/list/index' });
				break;
			}
			case 'service': {
				this.openMakePhone();
				break;
			}
			case 'help-center': {
				Toast({
					context: this,
					selector: '#t-toast',
					message: '你点击了帮助中心',
					icon: '',
					duration: 1000,
				});
				break;
			}
			case 'point': {
				Toast({
					context: this,
					selector: '#t-toast',
					message: '你点击了积分菜单',
					icon: '',
					duration: 1000,
				});
				break;
			}
			case 'coupon': {
				wx.navigateTo({ url: '/pages/coupon/coupon-list/index' });
				break;
			}
			default: {
				Toast({
					context: this,
					selector: '#t-toast',
					message: '未知跳转',
					icon: '',
					duration: 1000,
				});
				break;
			}
		}
	},

	jumpNav(e) {
		const status = e.detail.tabType;
		console.log('订单item点击：', e.detail);
		wx.navigateTo({ url: `/pages/order/order-list/index?status=${status}` });
		//售后的地址
		//wx.navigateTo({ url: '/pages/order/after-service-list/index' });
	},

	jumpAllOrder() {
		wx.navigateTo({ url: '/pages/order/order-list/index' });
	},

	openMakePhone() {
		this.setData({ showMakePhone: true });
	},

	closeMakePhone() {
		this.setData({ showMakePhone: false });
	},

	call() {
		wx.makePhoneCall({
			phoneNumber: this.data.customerServiceInfo.servicePhone,
		});
	},

	gotoUserEditPage() {
		const { currAuthStep } = this.data;
		if (currAuthStep === 2) {
			wx.navigateTo({ url: '/pages/usercenter/person-info/index' });
		} else {
			wx.navigateTo({
				url: `/pages/login/index`,
			});
		}
	},

	getVersionInfo() {
		const versionInfo = wx.getAccountInfoSync();
		const { version, envVersion = __wxConfig } = versionInfo.miniProgram;
		this.setData({
			versionNo: envVersion === 'release' ? version : envVersion,
		});
	},
	loginOut() {
		wx.showLoading({
			title: '处理中',
			mask: true,
		});
		wx.removeStorageSync('token');
		const me = this;
		setTimeout(() => {
			wx.hideLoading();
			app.globalData.isLogin = false;
			me.setData({ isLogin: false, currAuthStep: 1, userInfo: {} });
		}, 500);
	},
});
